package com.yaoli;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by YaoLi on 2017/8/22.
 */
public class LiuJi extends Thread{
    public int begin;
    public int end;


    public static void main(String[] args) throws Exception {

        for (int i = 1; i < 10; i++) {
            LiuJi liuJi = new LiuJi();
            liuJi.begin = i * 10000;
            liuJi.end = liuJi.begin + 10000 - 1;
            liuJi.start();
        }



    }

    @Override
    public void run() {
        calc(begin,end);
    }

    public void calc(int begin , int end){

        Queue<Integer> queue = new LinkedList<>();
        for (int i = begin; i <= end; i++) {
            queue.add(i);
        }

        while(queue.size() != 0){
            int ser = queue.poll();

            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                HttpGet httpget = new HttpGet("http://www.chsi.com.cn/cet/query?zkzh=3220111712"+ser+"&xm=%E8%B5%B5%E9%9B%A8%E9%9C%B2");
                httpget.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
                httpget.setHeader("Accept-Encoding","gzip, deflate");
                httpget.setHeader("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6,ja;q=0.4");
                httpget.setHeader("Host","www.chsi.com.cn");
                httpget.setHeader("Referer","http://www.chsi.com.cn/cet/");
                httpget.setHeader("Upgrade-Insecure-Requests","1");
                httpget.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36");
                httpget.setHeader("Cookie","JSESSIONID=FFAA9DDCF3AEDE88F1024BB3BF2977E5; aliyungf_tc=AQAAAO8gnEEhbAIAmXpmylnV8jiKvWFS; acw_tc=AQAAANdo7jZdrAIAmXpmypzPRNz33VTW; __utmt=1; __utma=65168252.10134491.1503364429.1503364429.1503367697.2; __utmb=65168252.1.10.1503367697; __utmc=65168252; __utmz=65168252.1503367697.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic");
                //System.out.println("executing request " + httpget.getURI());
                // 执行get请求.
                CloseableHttpResponse response = httpclient.execute(httpget);
                try {
                    // 获取响应实体
                    HttpEntity entity = response.getEntity();
                    //System.out.println("--------------------------------------");
                    // 打印响应状态
                    System.out.println(response.getStatusLine());
                    if (entity != null) {
                        String entityStr = EntityUtils.toString(entity);
                        if(entityStr.contains("数据库连接")){
                            System.out.println("Database Exception : "+ser);
                            queue.add(ser);
                        }else if(entityStr.contains("无法找到对应的分数")){
                            System.out.println("NO number : "+ser);
                            queue.add(ser);
                        }else{
                            System.out.println(entityStr);
                        }

                    }
                    //System.out.println("------------------------------------");
                } finally {
                    response.close();
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭连接,释放资源
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
